home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / Shark Attack / Sources & Headers / SpriteCollideProcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-22  |  5.9 KB  |  199 lines  |  [TEXT/CWIE]

  1. #include <SWIncludes.h>
  2. #include "Shark Attack.h"
  3. #include "NewSprite.h"
  4. #include "Level.h"
  5. #include "SpriteMoveProcs.h"
  6. #include "SpriteCollideProcs.h"
  7. #include "SWSounds.h"
  8. #include "GlobalVariables.h"
  9.  
  10.  
  11. #define kFishHitMoveDelay        4        // How many frames to delay a fish that was hit
  12. #define kSharkHitMoveDelay        5        // How many frames to delay a shark that was hit
  13.  
  14. #define kFishKilledScore        25
  15. #define kSharkKilledScore        50
  16. #define kFishHitScore            1
  17. #define kSharkHitScore            2
  18.  
  19. #define kFlashWhiteTime            2        // How many frames a fish stays white when hit
  20.  
  21.  
  22. ///--------------------------------------------------------------------------------------
  23. //   SubSpriteCollideProc
  24. ///--------------------------------------------------------------------------------------
  25.  
  26. SW_FUNC void SubSpriteCollideProc(
  27.     SpritePtr    subSpriteP,
  28.     SpritePtr    fishSpriteP,
  29.     Rect*        sectRect)
  30. {
  31.     #pragma            unused(sectRect)    // Tell CodeWarrior this variable is unused
  32.     short            oldFPSValue, stereoPos;
  33.     Boolean            hasCollided;
  34.     
  35.         // See if the actual sprite images collided
  36.     if (gSpriteWorldP->pixelDepth == 8)
  37.         hasCollided = SWPixelCollision(subSpriteP, fishSpriteP);    // Faster, but 8-bit only
  38.     else
  39.         hasCollided = SWRegionCollision(subSpriteP, fishSpriteP);    // Works in any depth
  40.  
  41.     if (hasCollided)
  42.     {
  43.         stereoPos = GetStereoPositionOfSprite(subSpriteP, &gSpriteWorldP->backRect);
  44.         PlaySound2(kSubDeadSnd, 2, kFindEmptyChannel, 256, stereoPos, k22khz, false);
  45.  
  46.             // Make all the sprites stop moving
  47.         PauseSprites();
  48.         
  49.             // Make the submarine sink to the bottom of the screen
  50.         SWSetSpriteMoveProc(subSpriteP, DeadSubSpriteMoveProc);
  51.         ((SubStructPtr)subSpriteP)->vertDelta = 0;
  52.         
  53.             // Allow the sub to sink smoothly & quickly to the bottom of the screen
  54.         oldFPSValue = gSpriteWorldP->fpsTimeInterval;
  55.         SWSetSpriteWorldMaxFPS(gSpriteWorldP, 90);
  56.         
  57.         gSubIsStillOnScreen = true;
  58.         while (gSubIsStillOnScreen)
  59.         {
  60.             SWProcessSpriteWorld(gSpriteWorldP);
  61.             SWAnimateSpriteWorld(gSpriteWorldP);
  62.         }
  63.         
  64.         UnpauseSprites();    // Unpause the SpriteLayers that were paused
  65.         
  66.             // Restore the original fps value
  67.         gSpriteWorldP->fpsTimeInterval = oldFPSValue;
  68.         
  69.         gNumLivesLeft--;
  70.         if (gNumLivesLeft < 0)
  71.         {
  72.             DoGameOver();
  73.             gGameOver = true;
  74.         }
  75.         else
  76.         {
  77.             RestartLevel();
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83. ///--------------------------------------------------------------------------------------
  84. //   FishSpriteCollideProc
  85. ///--------------------------------------------------------------------------------------
  86.  
  87. SW_FUNC void FishSpriteCollideProc(
  88.     SpritePtr    fishSpriteP,
  89.     SpritePtr    bulletSpriteP,
  90.     Rect*        sectRect)
  91. {
  92.     #pragma            unused(sectRect)    // Tell CodeWarrior this variable is unused
  93.     Boolean            hasCollided;
  94.     short            stereoPos;
  95.     FishStructPtr    fishStructP = (FishStructPtr)fishSpriteP;
  96.     
  97.         // See if the actual sprite images collided
  98.     if (gSpriteWorldP->pixelDepth == 8)
  99.         hasCollided = SWPixelCollision(bulletSpriteP, fishSpriteP);    // Faster, but 8-bit only
  100.     else
  101.         hasCollided = SWRegionCollision(bulletSpriteP, fishSpriteP);    // Works in any depth
  102.  
  103.  
  104.     if (hasCollided)
  105.     {
  106.             // Remove the bullet
  107.         ((BulletStructPtr)bulletSpriteP)->parentStructP->numBulletsOnScreen--;
  108.         SWRemoveSpriteFromAnimation(gSpriteWorldP, bulletSpriteP, true);
  109.         if (bulletSpriteP == gLastBulletP)
  110.             gLastBulletP = NULL;
  111.         
  112.         fishStructP->energy--;
  113.         
  114.             // Remove the fish if all energy is gone
  115.         if ( fishStructP->energy <= 0)
  116.         {
  117.             stereoPos = GetStereoPositionOfSprite(fishSpriteP, &gSpriteWorldP->backRect);
  118.             PlaySound2(kFishDeadSnd, 0, kFindEmptyChannel, 256, stereoPos, k22khz, false);
  119.             gScore += kFishKilledScore;
  120.             gNumFishOnScreen--;
  121.             SWRemoveSpriteFromAnimation(gSpriteWorldP, fishSpriteP, true);
  122.         }
  123.         else
  124.         {
  125.             stereoPos = GetStereoPositionOfSprite(fishSpriteP, &gSpriteWorldP->backRect);
  126.             PlaySound2(kHitFishSnd, 2, kFindEmptyChannel, 256, stereoPos, k22khz, false);
  127.             
  128.             gScore += kFishHitScore;
  129.             
  130.                 // Make the fish pause temporarily because it was hit
  131.             fishStructP->moveDelay = kFishHitMoveDelay;
  132.             
  133.                 // Make the fish flash white
  134.             fishStructP->hitCounter = kFlashWhiteTime;
  135.             fishSpriteP->needsToBeDrawn = true;
  136.             SWSetSpriteDrawProc(fishSpriteP, FishHitDrawProc);
  137.         }
  138.     }
  139. }
  140.  
  141.  
  142. ///--------------------------------------------------------------------------------------
  143. //   SharkSpriteCollideProc
  144. ///--------------------------------------------------------------------------------------
  145.  
  146. SW_FUNC void SharkSpriteCollideProc(
  147.     SpritePtr    sharkSpriteP,
  148.     SpritePtr    bulletSpriteP,
  149.     Rect*        sectRect)
  150. {
  151.     #pragma            unused(sectRect)    // Tell CodeWarrior this variable is unused
  152.     Boolean            hasCollided;
  153.     short            stereoPos;
  154.     SharkStructPtr    sharkStructP = (SharkStructPtr)sharkSpriteP;
  155.     
  156.         // See if the actual sprite images collided
  157.     if (gSpriteWorldP->pixelDepth == 8)
  158.         hasCollided = SWPixelCollision(bulletSpriteP, sharkSpriteP);    // Faster, but 8-bit only
  159.     else
  160.         hasCollided = SWRegionCollision(bulletSpriteP, sharkSpriteP);    // Works in any depth
  161.  
  162.  
  163.     if (hasCollided)
  164.     {
  165.             // Remove the bullet
  166.         ((BulletStructPtr)bulletSpriteP)->parentStructP->numBulletsOnScreen--;
  167.         SWRemoveSpriteFromAnimation(gSpriteWorldP, bulletSpriteP, true);
  168.         if (bulletSpriteP == gLastBulletP)
  169.             gLastBulletP = NULL;
  170.         
  171.         sharkStructP->energy--;
  172.         
  173.             // Remove the shark if all energy is gone
  174.         if ( sharkStructP->energy <= 0)
  175.         {
  176.             stereoPos = GetStereoPositionOfSprite(sharkSpriteP, &gSpriteWorldP->backRect);
  177.             PlaySound2(kSharkDeadSnd, 1, kFindEmptyChannel, 256, stereoPos, k22khz, false);
  178.             gScore += kSharkKilledScore;
  179.             gNumSharksOnScreen--;
  180.             SWRemoveSpriteFromAnimation(gSpriteWorldP, sharkSpriteP, true);
  181.         }
  182.         else
  183.         {
  184.             stereoPos = GetStereoPositionOfSprite(sharkSpriteP, &gSpriteWorldP->backRect);
  185.             PlaySound2(kHitSharkSnd, 2, kFindEmptyChannel, 256, stereoPos, k22khz, false);
  186.             gScore += kSharkHitScore;
  187.             
  188.                 /// Make the shark pause temporarily because it was hit
  189.             sharkStructP->moveDelay = kSharkHitMoveDelay;
  190.             
  191.                 // Make the shark flash white
  192.             sharkStructP->hitCounter = kFlashWhiteTime;
  193.             sharkSpriteP->needsToBeDrawn = true;
  194.             SWSetSpriteDrawProc(sharkSpriteP, FishHitDrawProc);
  195.         }
  196.     }
  197. }
  198.  
  199.